#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Stu
{
	char name[20];
	int age;
};

void Swap(char* buf1, char* buf2, int width)
{
	int i = 0;
	for (i = 0; i < width; i++)
	{
		char tmp = *buf1;
		*buf1 = *buf2;
		*buf2 = tmp;
		buf1++;
		buf2++;
	}
}

void cmp_int(const void* p1, const void* p2)
{
	return *((int*)p1) - *((int*)p2);
}

void stu_cmp_char(const void* p1, const void* p2)
{
	return strcmp(((struct Stu*)p1)->name, ((struct Stu*)p2)->name);
}

void stu_cmp_int(const void* p1, const void* p2)
{
	return (((struct Stu*)p1)->age) - (((struct Stu*)p2)->age);
}


void Bubble_Sort_any(const void* arr, int sz, int width, int (*cmp)(const void*, const void*))
{
	int i = 0;
	for (i = 0; i < sz - 1; i++)
	{
		int j = 0;
		for (j = 0; j < sz - i -1; j++)
		{
			if (cmp(((char*)arr) + width * j, ((char*)arr) + width * (j + 1)) > 0)
			{
				Swap(((char*)arr) + width * j, ((char*)arr) + width * (j + 1), width);
			}
		}
	}
}

void int_print(int* arr, int sz)
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%d ", *(arr + i));
	}
}

void test_int()
{
	int arr[10] = { 0,5,7,2,3,4,9,1,8,6 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	Bubble_Sort_any(arr, sz, sizeof(arr[0]), cmp_int);
	int_print(arr, sz);
}

void test_stu_char()
{
	struct Stu arr[3] = { {"zhangsan", 20},{"lisi",25},{"wangwu",18} };
	int sz = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, sz, sizeof(arr[0]), stu_cmp_char);
}

void test_stu_int()
{
	struct Stu arr[3] = { {"zhangsan", 20},{"lisi",25},{"wangwu",18} };
	int sz = sizeof(arr) / sizeof(arr[0]);
	Bubble_Sort_any(arr, sz, sizeof(arr[0]), stu_cmp_int);

}

int main()
{
	test_stu_char();
	//test_stu_int();
	//test_int();
	return 0;
}